home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / scripts / linear-algebra / norm.m < prev    next >
Text File  |  1996-07-12  |  3KB  |  116 lines

  1. ## Copyright (C) 1996 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## usage: norm (x, p)
  21. ##
  22. ## Compute the p-norm of x.
  23. ##
  24. ## If x is a matrix:
  25. ##
  26. ##   value of p     norm returns
  27. ##   ----------     ------------
  28. ##       1          1-norm, the largest column sum of x
  29. ##       2          largest singular value of x
  30. ##      Inf         infinity norm, the largest row sum of x
  31. ##     "inf"        same as Inf
  32. ##     "fro"        Frobenius norm of x, sqrt (sum (diag (x' * x)))
  33. ##
  34. ## If x is a vector or a scalar:
  35. ##
  36. ##   value of p     norm returns
  37. ##   ----------     ------------
  38. ##      Inf         max (abs (x))
  39. ##     -Inf         min (abs (x))
  40. ##     other        p-norm of x, sum (abs (x) .^ p) ^ (1/p)
  41. ##
  42. ## If the second argument is missing, p = 2 is assumed.
  43. ##
  44. ## See also: cond, svd
  45.  
  46. ## Author: jwe
  47.  
  48. function retval = norm (x, p)
  49.  
  50.   if (nargin < 1 || nargin > 2)
  51.     error ("usage: norm (x [, p])");
  52.   endif
  53.  
  54.   if (isempty (x))
  55.     retval = [];
  56.     return;
  57.   endif
  58.  
  59.   ## Do we have a vector or matrix as the first argument?
  60.  
  61.   if (rows (x) == 1 || columns (x) == 1)
  62.  
  63.     if (nargin == 2)
  64.       if (isstr (p))
  65.         if (strcmp (p, "fro"))
  66.           retval = sqrt (sum (diag (x' * x)));
  67.         elseif (strcmp (p, "inf"))
  68.           retval = max (abs (x));
  69.         else
  70.           error ("norm: unrecognized norm");
  71.         endif
  72.       else
  73.     if (p == Inf)
  74.       retval = max (abs (x));
  75.     elseif (p == -Inf)
  76.       retval = min (abs (x));
  77.     else
  78.       retval = sum (abs (x) .^ p) ^ (1/p);
  79.     endif
  80.       endif
  81.     elseif (nargin == 1)
  82.       retval = sum (abs (x) .^ 2) ^ 0.5;
  83.     endif
  84.  
  85.   else
  86.  
  87.     if (nargin == 2)
  88.       if (isstr (p))
  89.         if (strcmp (p, "fro"))
  90.           retval = sqrt (sum (diag (x' * x)));
  91.         elseif (strcmp (p, "inf"))
  92.           xp = x';
  93.           retval = max (sum (abs (real (xp)) + abs (imag (xp))));
  94.         else
  95.           error ("norm: unrecognized norm");
  96.         endif
  97.       else
  98.         if (p == 1)
  99.           retval = max (sum (abs (real (x)) + abs (imag (x))));
  100.         elseif (p == 2)
  101.           s = svd (x);
  102.           retval = s (1);
  103.         elseif (p == Inf)
  104.           xp = x';
  105.           retval = max (sum (abs (real (xp)) + abs (imag (xp))));
  106.         endif
  107.       endif
  108.     elseif (nargin == 1)
  109.       s = svd (x);
  110.       retval = s (1);
  111.     endif
  112.  
  113.   endif
  114.  
  115. endfunction
  116.